<?php
/**
 * Created by PhpStorm.
 * User: LYD
 * Date: 2019/2/2
 * Time: 21:33
 */
//数据库类
class db{
    private $_conn;  //保存数据库连接成功句柄
    private $_servername = "localhost";
    private $_username = "root";
    private $_password = "";
    private $_dbData = "ts";
    private $_sqlRuse = array();
    private $_AsqlRuse = array();
//    public $_se_yhm;
//    public $_se_qx;
    public $limit;
    //连接数据库方法
    private function db_conn(){
        $_conn = new mysqli($this->_servername,$this->_username,$this->_password,$this->_dbData);
        if($_conn->connect_error){
            exit("数据库连接失败");
        }
        $_conn->set_charset("utf8");
        $this->_conn = $_conn;
    }

    //销毁数据库连接
    private function db_destroy(){
        mysqli_close($this->_conn);
    }

    //查询数据表中的所有数据，用做统计数据条数
    public function db_query(){
        $this->db_conn();
        $_sql = "SELECT * FROM user";
        $_mysqli_query = $this->_conn->query($_sql);
        while ($_a = $_mysqli_query->fetch_assoc()){
            $this->_AsqlRuse[] = $_a;
        }
        $this->db_destroy();
        return $this->_AsqlRuse;
    }
    //查询数据表中以条件查询数据，用做统计数据条数
    public function db_queryGLY($_gly){
        $this->db_conn();
        $_sql = "SELECT * FROM user where permissions = '$_gly'";
        $_mysqli_query = $this->_conn->query($_sql);
        while ($_a = $_mysqli_query->fetch_assoc()){
            $this->_AsqlRuse[] = $_a;
        }
        $this->db_destroy();
        return $this->_AsqlRuse;
    }

    //查询数据表中的数据，显示9条数据
    public function db_Nquery(){
        $this->db_conn();
        $_sql = "SELECT * FROM user ORDER BY id ASC $this->limit";
        $_mysqli_query = $this->_conn->query($_sql);
        while ($_a = $_mysqli_query->fetch_assoc()){
            $this->_sqlRuse[] = $_a;
        }
        $this->db_destroy();
        return $this->_sqlRuse;
    }

    //查询数据表中的超级管理员数据，显示9条数据
    public function db_NqueryGLY($_gly){
        $this->db_conn();
        $_sql = "SELECT * FROM user where permissions ='$_gly' ORDER BY id ASC $this->limit";
        $_mysqli_query = $this->_conn->query($_sql);
        while ($_a = $_mysqli_query->fetch_assoc()){
            $this->_sqlRuse[] = $_a;
        }
        $this->db_destroy();
        return $this->_sqlRuse;
    }

    //添加管理员数据
    public function add(){
        $this->db_conn();
        if(!empty($_POST["sub_add"])){
            $username = $_POST["username"];
            $account =$_POST["account"];
            $password = $_POST["pass"];
            $select_value = $_POST["select"];
            if((!empty($username) && !empty($account)) && (!empty($password) && $select_value != "0")){
                $_sql = "INSERT into user 
                        (id, username, account, password, permissions, regtime, lognumber, lasttime)
                        values 
                        (null, '$username', '$account', '$password', '$select_value', now(), '0', now())";
                if ($this->_conn->query($_sql) === TRUE) { //插入数据
                    echo "<script>alert('添加成功');</script>>";
                    echo "<script> location.replace(location.href);</script>";
                }else{
                    exit("Error: " . $_sql . "<br>" . $this->_conn->error);//插入数据错误信息
                }
            }
        }
        $this->db_destroy();
    }

    //查询单个数据，显示在修改弹窗上
    public function mod(){
        $this->db_conn();
        if(!empty(@$_GET['id']) && @($_GET['action'] == mod)){
            $isid = $_GET['id'];
            $query_sql = "select * from user where id = '$isid'";
            $query_sj = $this->_conn->query($query_sql);
            $is = $query_sj->fetch_assoc();
            $this->db_destroy();
            return $is;
        }
    }

    //修改管理员
    public function modify(){
        $this->db_conn();
        if(!empty($_POST["sub_mod"])){
            $mod_username = $_POST["mod-username"];
            $mod_account = $_POST["mod-account"];
            $mod_pass = $_POST["mod-pass"];
            $mod_select = $_POST["mod-select"];
            $mod_id = $_POST["mod_id"];
            if((!empty($mod_username) && !empty($mod_account)) && (!empty($mod_pass) && $mod_select != "0")){
                $sql = "update user set
                        username = '$mod_username',account = '$mod_account',password = '$mod_pass',permissions = '$mod_select'
                        where id = '$mod_id'";
                if ($this->_conn->query($sql) === TRUE) { //插入数据
                    echo "<script>alert('修改成功');</script>>";
                    echo "<script> location.replace(location.href);</script>";
                }else{
                    exit("Error: " . $sql . "<br>" . $this->_conn->error);//插入数据错误信息
                }
            }
        }
        $this->db_destroy();
    }

    //删除管理员数据
    public function delete(){
        $this->db_conn();
        if(!empty(@$_GET['id']) && @($_GET['action'] == del)){
            $get_id = @$_GET['id'];
            $_sql = "delete from user where id='$get_id'";
            if ($this->_conn->query($_sql) === TRUE) { //删除数据成功
                echo "<script>alert('删除成功');</script>>";
                echo "<script> location.replace('../admin/book_de.php');</script>";
                return;
            }else{
                exit("Error: " . $_sql . "<br>" . $this->_conn->error);//插入数据错误信息
            }

        }
        $this->db_destroy();
    }

    //验证登陆表单的数据
    public function login_from(){
        if(!empty($_POST['login_sub'])){
            $this->db_conn();
            $login_account = $_POST['login_account'];
            $login_pass = $_POST['login_pass'];
            $sql = "select username,permissions from user where account = '$login_account' and password = '$login_pass'";
            $login_sel = $this->_conn->query($sql)->fetch_assoc(); //获取到的数据
            if($login_sel){
                $_SESSION['admin']['username'] = $login_sel['username'];
                $_SESSION['admin']['permissions'] = $login_sel['permissions'];
//                $this->_se_yhm = $_SESSION['admin']['username'];
//                $this->_se_qx = $_SESSION['admin']['permissions'];
                header("location: ../admin/admin.php");//跳转页面，并刷新跳转的页面
            }else{
                echo "<script>alert('账号或密码错误!');</script>";
            }
            return $login_sel;
            $this->db_destroy();
        }
    }
}
?>